/**
 * 18位随机数
 * @param {Object} seconds
 */
export function generateRandomNumber() {
  let result = '';
  for (let i = 0; i < 18; i++) {
    result += Math.floor(Math.random() * 10); // 生成 0 到 9 之间的随机整数
  }
  return result;
}
/* 文字对象提取 */
export function getWordsToArray(value){
	const words = JSON.parse(value)
	const arr = [];
	for(let key in words) {
		arr.push(words[key])
	}
	return arr
}

/* 字符转对象*/
export function wordsToObject(value){
	const obj = {}
	value.forEach((item, index) => {
		obj[index] = item.text
	})
	const words = `${JSON.stringify(obj)}`
	return words
}
/* 字符转数组*/
export function stringToArray(value, conn = '、'){
	if(!value || value && !value.length) {
		return []
	}
	// 集合
	const textItem = []
	value.forEach(text => {
		if(text !== "" && text !== conn) {
			const obj = {
				text,
				id: generateRandomNumber()
			}
			textItem.push(obj)
		}
		
	})
	return textItem;
}

/**
 * 文本分割为数组，并生成JSON对象
 */
export function wordsSplitToArray(value, symbol = '、'){
	if(!value) {
		return
	}
	// 分割
	const splitText = value.split(symbol);
	// 生成对象
	// 集合
	const textItem = []
	splitText.forEach(text => {
		const obj = {
			text,
			id: generateRandomNumber()
		}
		textItem.push(obj)
	})
	return textItem;
}
export function isPoetry(category){
	return (category * 1) === 15
}
/**
 * 古诗
 */
export function libraryPoetryToObject(wordsItem){
	const array = [];
	wordsItem.forEach(item => {
		const words = JSON.parse(item);
		array.push(words)
	})
	return array
}

/**
 * 数据原始数据提取文字
 * { "256|1":"珠" }
 */
export function libraryWordsToObject(wordsItem, custom = false){
	let result = [];
	if(custom) {
		result = wordsToArray(wordsItem)
		return result
	}
	wordsItem.forEach(item => {
		const words = JSON.parse(item);
		for(let key in words) {
			const splitId = key.split('|')
			const obj = {
				text: words[key],
				rowId: splitId[0],
				id: splitId[1],
			}
			result.push(obj)
		}
	})
	return result;
}
/**
 * 文字转数组，数据库的文字
 * @param {Object} value
 */
export function wordsToArray(value){
	const words = JSON.parse(value)
	const arr = [];
	for(let key in words) {
		const obj = {
			id: key,
			text: words[key]
		}
		arr.push(obj)
	}
	return arr
}

// 获取文本code
export function textToUnicode(text) {
	const code = '\\u' + text.charCodeAt(0).toString(16);
	return code
}
// 获取指定范围随机数
export function getRandomNumber(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
// 提取文字尖
export function getWordsId(words){
	if(!words) {
		return null
	}
	const ids = words.id.split("|")
	if(ids.length === 1) {
		return {
			...words,
			id: ids[0],
			words: words.text,
			disabled: words.disabled,
		}
	}
	return {
		rowId: ids[0],
		id: ids[1],
		words: words.text
	}
}
/** 保留中文、英文、数字、空格*/
export function getAudioText(value){
	return value.replace(/[^\u4e00-\u9fa5a-zA-Z0-9 ]/g, '');
}
/** 判断是否26个字母*/
export function isLetterAndNumber(str) {
    return /[A-Za-z0-9]/.test(str);
}
/** 数组打乱排序*/
export function shuffleArray(array) {
  array.sort(() => Math.random() - 0.5);
  return array;
}
/** 正确率 */
export function calculateAccuracy(totalQuestions, incorrectQuestions = 0) {
    const correctQuestions = totalQuestions - incorrectQuestions;
    const accuracy = (correctQuestions / totalQuestions) * 100;
    return accuracy;
}
/** 得分 */
export function calculateScore(totalQuestions, wrongAnswers) {
    const correctAnswers = totalQuestions - wrongAnswers; // 计算正确答案数量
    const scorePerQuestion = 100 / totalQuestions; // 计算每道题的分数
    const finalScore = correctAnswers * scorePerQuestion; // 计算总分
	const isFixed = String(finalScore).indexOf('.') !== -1
    return isFixed ? finalScore.toFixed(1) : finalScore;
}
/** 获取数组随机，指定的多项 */
export function getArrayRandomItems(arr, num) {
	const clone = [...arr]; // 创建副本避免修改原数组
	const len = clone.length;
	// 执行num次交换以随机化前num个元素
	for (let i = 0; i < num; i++) {
	  const randIndex = Math.floor(Math.random() * (len - i)) + i;
	  [clone[i], clone[randIndex]] = [clone[randIndex], clone[i]];
	}
	return clone.slice(0, num); // 返回前num个元素
  }